#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++) 
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;

int read() {
	int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-', ch = getchar(); 
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return f ? -x : x;
}

const int N = 1e5 + 10;

int dis[N], fa[N], siz[N], sum[N][3], n, m, tp[N], lcnt[N];
ll res[N], ans;

void gf(int &x, int &c) { while(x ^ fa[x]) (c += dis[x]) %= 3, x = fa[x]; }

void upd(int x) {
	if(tp[x]) res[x] = 1ll * siz[x] * siz[x];
	else if(sum[x][0] && sum[x][1] && sum[x][2]) { res[x] = 0; rep(i, 0, 2) res[x] += 1ll * sum[x][i] * sum[x][(i + 1) % 3]; }
	else res[x] = lcnt[x];
}

void merge(int x, int y) {
	int cx = 0, cy = 0;
	gf(x, cx), gf(y, cy);
	if(x == y) {
		if((cx + 1) % 3 != cy) tp[x] = 1;
		ans -= res[x]; lcnt[x]++; upd(x); ans += res[x];
	} else {
		int mdis = 1;
		if(siz[x] < siz[y]) swap(x, y), swap(cx, cy), mdis = 2;
		ans -= res[x]; ans -= res[y]; lcnt[x] += lcnt[y] + 1;
		fa[y] = x; dis[y] = (mdis + cx - cy + 3) % 3; if(tp[y]) tp[x] = 1;
		rep(i, 0, 2) sum[x][i] += sum[y][(i + dis[y]) % 3]; siz[x] += siz[y];
		upd(x); ans += res[x];
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in, m = in;
	rep(i, 1, n) fa[i] = i, sum[i][0] = 1, siz[i] = 1;
	rep(i, 1, m) merge(in, in); printf("%lld\n", ans);
	return 0;
}
